﻿#region Using Directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;

using PAT.Entities;
using PAT.Data;
using PAT.Data.Bases;
using PAT.Services;
#endregion

namespace PAT.Web.Data
{
	/// <summary>
	/// Represents the DataRepository.PatOrdenTrabajoOperacionProvider object that provides
	/// data to data-bound controls in multi-tier Web application architectures.
	/// </summary>
	[Designer(typeof(PatOrdenTrabajoOperacionDataSourceDesigner))]
	public class PatOrdenTrabajoOperacionDataSource : ProviderDataSource<PatOrdenTrabajoOperacion, PatOrdenTrabajoOperacionKey>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatOrdenTrabajoOperacionDataSource class.
		/// </summary>
		public PatOrdenTrabajoOperacionDataSource() : base(new PatOrdenTrabajoOperacionService())
		{
		}

		#endregion Constructors
		
		#region Properties
		
		/// <summary>
		/// Gets a reference to the PatOrdenTrabajoOperacionDataSourceView used by the PatOrdenTrabajoOperacionDataSource.
		/// </summary>
		protected PatOrdenTrabajoOperacionDataSourceView PatOrdenTrabajoOperacionView
		{
			get { return ( View as PatOrdenTrabajoOperacionDataSourceView ); }
		}
		
		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the PatOrdenTrabajoOperacionDataSource control invokes to retrieve data.
		/// </summary>
		public PatOrdenTrabajoOperacionSelectMethod SelectMethod
		{
			get
			{
				PatOrdenTrabajoOperacionSelectMethod selectMethod = PatOrdenTrabajoOperacionSelectMethod.GetAll;
				Object method = ViewState["SelectMethod"];
				if ( method != null )
				{
					selectMethod = (PatOrdenTrabajoOperacionSelectMethod) method;
				}
				return selectMethod;
			}
			set { ViewState["SelectMethod"] = value; }
		}

		#endregion Properties
		
		#region Methods

		/// <summary>
		/// Creates a new instance of the PatOrdenTrabajoOperacionDataSourceView class that is to be
		/// used by the PatOrdenTrabajoOperacionDataSource.
		/// </summary>
		/// <returns>An instance of the PatOrdenTrabajoOperacionDataSourceView class.</returns>
		protected override BaseDataSourceView<PatOrdenTrabajoOperacion, PatOrdenTrabajoOperacionKey> GetNewDataSourceView()
		{
			return new PatOrdenTrabajoOperacionDataSourceView(this, DefaultViewName);
		}
		
		/// <summary>
        /// Creates a cache hashing key based on the startIndex, pageSize and the SelectMethod being used.
        /// </summary>
        /// <param name="startIndex">The current start row index.</param>
        /// <param name="pageSize">The current page size.</param>
        /// <returns>A string that can be used as a key for caching purposes.</returns>
		protected override string CacheHashKey(int startIndex, int pageSize)
        {
			return String.Format("{0}:{1}:{2}", SelectMethod, startIndex, pageSize);
        }
		
		#endregion Methods
	}
	
	/// <summary>
	/// Supports the PatOrdenTrabajoOperacionDataSource control and provides an interface for
	/// data-bound controls to perform data operations with business and data objects.
	/// </summary>
	public class PatOrdenTrabajoOperacionDataSourceView : ProviderDataSourceView<PatOrdenTrabajoOperacion, PatOrdenTrabajoOperacionKey>
	{
		#region Declarations

		#endregion Declarations
		
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatOrdenTrabajoOperacionDataSourceView class.
		/// </summary>
		/// <param name="owner">A reference to the PatOrdenTrabajoOperacionDataSource which created this instance.</param>
		/// <param name="viewName">The name of the view.</param>
		public PatOrdenTrabajoOperacionDataSourceView(PatOrdenTrabajoOperacionDataSource owner, String viewName)
			: base(owner, viewName)
		{
		}
		
		#endregion Constructors
		
		#region Properties

		/// <summary>
		/// Gets a strongly-typed reference to the Owner property.
		/// </summary>
		internal PatOrdenTrabajoOperacionDataSource PatOrdenTrabajoOperacionOwner
		{
			get { return Owner as PatOrdenTrabajoOperacionDataSource; }
		}

		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the DataSource control invokes to retrieve data.
		/// </summary>
		internal PatOrdenTrabajoOperacionSelectMethod SelectMethod
		{
			get { return PatOrdenTrabajoOperacionOwner.SelectMethod; }
			set { PatOrdenTrabajoOperacionOwner.SelectMethod = value; }
		}

		/// <summary>
		/// Gets a strongly typed reference to the Provider property.
		/// </summary>
		internal PatOrdenTrabajoOperacionService PatOrdenTrabajoOperacionProvider
		{
			get { return Provider as PatOrdenTrabajoOperacionService; }
		}

		#endregion Properties
		
		#region Methods
		
		/// <summary>
		/// Gets a collection of Entity objects based on the value of the SelectMethod property.
		/// </summary>
		/// <param name="count">The total number of rows in the DataSource.</param>
		/// <returns>A collection of Entity objects.</returns>
		protected override IList<PatOrdenTrabajoOperacion> GetSelectData(out int count)
		{
			Hashtable values = CollectionsUtil.CreateCaseInsensitiveHashtable(GetParameterValues());
			Hashtable customOutput = CollectionsUtil.CreateCaseInsensitiveHashtable();
			IList<PatOrdenTrabajoOperacion> results = null;
			PatOrdenTrabajoOperacion item;
			count = 0;
			
			System.Int32 _idDocumento;
			System.Int32 _numeroOrden;
			System.Int32 _idOperacion;
			System.Byte _idCargoOperacion;
			System.Int16? _idCausalGarantia1_nullable;
			System.Int16? _idCausalGarantia2_nullable;
			System.Int16? _idCausalGarantia3_nullable;
			System.Int16? _idCausalGarantia4_nullable;
			System.Int32? _idEmpleado_nullable;
			System.Byte? _idEstadoOperacion_nullable;

			switch ( SelectMethod )
			{
				case PatOrdenTrabajoOperacionSelectMethod.Get:
					PatOrdenTrabajoOperacionKey entityKey  = new PatOrdenTrabajoOperacionKey();
					entityKey.Load(values);
					item = PatOrdenTrabajoOperacionProvider.Get(entityKey);
					results = new TList<PatOrdenTrabajoOperacion>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				case PatOrdenTrabajoOperacionSelectMethod.GetAll:
                    results = PatOrdenTrabajoOperacionProvider.GetAll(StartIndex, PageSize, out count);
                    break;
				case PatOrdenTrabajoOperacionSelectMethod.GetPaged:
					results = PatOrdenTrabajoOperacionProvider.GetPaged(WhereClause, OrderBy, PageIndex, PageSize, out count);
					break;
				case PatOrdenTrabajoOperacionSelectMethod.Find:
					if ( FilterParameters != null )
						results = PatOrdenTrabajoOperacionProvider.Find(FilterParameters, OrderBy, StartIndex, PageSize, out count);
					else
						results = PatOrdenTrabajoOperacionProvider.Find(WhereClause, StartIndex, PageSize, out count);
                    break;
				// PK
				case PatOrdenTrabajoOperacionSelectMethod.GetByIdDocumentoNumeroOrdenIdOperacion:
					_idDocumento = ( values["IdDocumento"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["IdDocumento"], typeof(System.Int32)) : (int)0;
					_numeroOrden = ( values["NumeroOrden"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["NumeroOrden"], typeof(System.Int32)) : (int)0;
					_idOperacion = ( values["IdOperacion"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["IdOperacion"], typeof(System.Int32)) : (int)0;
					item = PatOrdenTrabajoOperacionProvider.GetByIdDocumentoNumeroOrdenIdOperacion(_idDocumento, _numeroOrden, _idOperacion);
					results = new TList<PatOrdenTrabajoOperacion>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				// IX
				// FK
				case PatOrdenTrabajoOperacionSelectMethod.GetByIdCargoOperacion:
					_idCargoOperacion = ( values["IdCargoOperacion"] != null ) ? (System.Byte) EntityUtil.ChangeType(values["IdCargoOperacion"], typeof(System.Byte)) : (byte)0;
					results = PatOrdenTrabajoOperacionProvider.GetByIdCargoOperacion(_idCargoOperacion, this.StartIndex, this.PageSize, out count);
					break;
				case PatOrdenTrabajoOperacionSelectMethod.GetByIdCausalGarantia1:
					_idCausalGarantia1_nullable = (System.Int16?) EntityUtil.ChangeType(values["IdCausalGarantia1"], typeof(System.Int16?));
					results = PatOrdenTrabajoOperacionProvider.GetByIdCausalGarantia1(_idCausalGarantia1_nullable, this.StartIndex, this.PageSize, out count);
					break;
				case PatOrdenTrabajoOperacionSelectMethod.GetByIdCausalGarantia2:
					_idCausalGarantia2_nullable = (System.Int16?) EntityUtil.ChangeType(values["IdCausalGarantia2"], typeof(System.Int16?));
					results = PatOrdenTrabajoOperacionProvider.GetByIdCausalGarantia2(_idCausalGarantia2_nullable, this.StartIndex, this.PageSize, out count);
					break;
				case PatOrdenTrabajoOperacionSelectMethod.GetByIdCausalGarantia3:
					_idCausalGarantia3_nullable = (System.Int16?) EntityUtil.ChangeType(values["IdCausalGarantia3"], typeof(System.Int16?));
					results = PatOrdenTrabajoOperacionProvider.GetByIdCausalGarantia3(_idCausalGarantia3_nullable, this.StartIndex, this.PageSize, out count);
					break;
				case PatOrdenTrabajoOperacionSelectMethod.GetByIdCausalGarantia4:
					_idCausalGarantia4_nullable = (System.Int16?) EntityUtil.ChangeType(values["IdCausalGarantia4"], typeof(System.Int16?));
					results = PatOrdenTrabajoOperacionProvider.GetByIdCausalGarantia4(_idCausalGarantia4_nullable, this.StartIndex, this.PageSize, out count);
					break;
				case PatOrdenTrabajoOperacionSelectMethod.GetByIdEmpleado:
					_idEmpleado_nullable = (System.Int32?) EntityUtil.ChangeType(values["IdEmpleado"], typeof(System.Int32?));
					results = PatOrdenTrabajoOperacionProvider.GetByIdEmpleado(_idEmpleado_nullable, this.StartIndex, this.PageSize, out count);
					break;
				case PatOrdenTrabajoOperacionSelectMethod.GetByIdEstadoOperacion:
					_idEstadoOperacion_nullable = (System.Byte?) EntityUtil.ChangeType(values["IdEstadoOperacion"], typeof(System.Byte?));
					results = PatOrdenTrabajoOperacionProvider.GetByIdEstadoOperacion(_idEstadoOperacion_nullable, this.StartIndex, this.PageSize, out count);
					break;
				case PatOrdenTrabajoOperacionSelectMethod.GetByIdDocumentoNumeroOrden:
					_idDocumento = ( values["IdDocumento"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["IdDocumento"], typeof(System.Int32)) : (int)0;
					_numeroOrden = ( values["NumeroOrden"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["NumeroOrden"], typeof(System.Int32)) : (int)0;
					results = PatOrdenTrabajoOperacionProvider.GetByIdDocumentoNumeroOrden(_idDocumento, _numeroOrden, this.StartIndex, this.PageSize, out count);
					break;
				case PatOrdenTrabajoOperacionSelectMethod.GetByIdOperacion:
					_idOperacion = ( values["IdOperacion"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["IdOperacion"], typeof(System.Int32)) : (int)0;
					results = PatOrdenTrabajoOperacionProvider.GetByIdOperacion(_idOperacion, this.StartIndex, this.PageSize, out count);
					break;
				// M:M
				// Custom
				default:
					break;
			}

			if ( results != null && count < 1 )
			{
				count = results.Count;

				if ( !String.IsNullOrEmpty(CustomMethodRecordCountParamName) )
				{
					object objCustomCount = EntityUtil.ChangeType(customOutput[CustomMethodRecordCountParamName], typeof(Int32));
					
					if ( objCustomCount != null )
					{
						count = (int) objCustomCount;
					}
				}
			}
			
			return results;
		}
		
		/// <summary>
		/// Gets the values of any supplied parameters for internal caching.
		/// </summary>
		/// <param name="values">An IDictionary object of name/value pairs.</param>
		protected override void GetSelectParameters(IDictionary values)
		{
			if ( SelectMethod == PatOrdenTrabajoOperacionSelectMethod.Get || SelectMethod == PatOrdenTrabajoOperacionSelectMethod.GetByIdDocumentoNumeroOrdenIdOperacion )
			{
				EntityId = GetEntityKey(values);
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation for the current entity if it has
		/// not already been performed.
		/// </summary>
		internal override void DeepLoad()
		{
			if ( !IsDeepLoaded )
			{
				PatOrdenTrabajoOperacion entity = GetCurrentEntity();
				
				if ( entity != null )
				{
					// init transaction manager
					GetTransactionManager();
					// execute deep load method
					PatOrdenTrabajoOperacionProvider.DeepLoad(GetCurrentEntity(), EnableRecursiveDeepLoad);
					// set loaded flag
					IsDeepLoaded = true;
				}
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation on the specified entity collection.
		/// </summary>
		/// <param name="entityList"></param>
		/// <param name="properties"></param>
		internal override void DeepLoad(TList<PatOrdenTrabajoOperacion> entityList, ProviderDataSourceDeepLoadList properties)
		{
			// init transaction manager
			GetTransactionManager();
			// execute deep load method
			PatOrdenTrabajoOperacionProvider.DeepLoad(entityList, properties.Recursive, properties.Method, properties.GetTypes());
		}

		#endregion Select Methods
	}
	
	#region PatOrdenTrabajoOperacionDataSourceDesigner

	/// <summary>
	/// Provides design-time support in a design host for the PatOrdenTrabajoOperacionDataSource class.
	/// </summary>
	public class PatOrdenTrabajoOperacionDataSourceDesigner : ProviderDataSourceDesigner<PatOrdenTrabajoOperacion, PatOrdenTrabajoOperacionKey>
	{
		/// <summary>
		/// Initializes a new instance of the PatOrdenTrabajoOperacionDataSourceDesigner class.
		/// </summary>
		public PatOrdenTrabajoOperacionDataSourceDesigner()
		{
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public PatOrdenTrabajoOperacionSelectMethod SelectMethod
		{
			get { return ((PatOrdenTrabajoOperacionDataSource) DataSource).SelectMethod; }
			set { SetPropertyValue("SelectMethod", value); }
		}

		/// <summary>Gets the designer action list collection for this designer.</summary>
		/// <returns>The <see cref="T:System.ComponentModel.Design.DesignerActionListCollection"/>
		/// associated with this designer.</returns>
		public override DesignerActionListCollection ActionLists
		{
			get
			{
				DesignerActionListCollection actions = new DesignerActionListCollection();
				actions.Add(new PatOrdenTrabajoOperacionDataSourceActionList(this));
				actions.AddRange(base.ActionLists);
				return actions;
			}
		}
	}

	#region PatOrdenTrabajoOperacionDataSourceActionList

	/// <summary>
	/// Supports the PatOrdenTrabajoOperacionDataSourceDesigner class.
	/// </summary>
	internal class PatOrdenTrabajoOperacionDataSourceActionList : DesignerActionList
	{
		private PatOrdenTrabajoOperacionDataSourceDesigner _designer;

		/// <summary>
		/// Initializes a new instance of the PatOrdenTrabajoOperacionDataSourceActionList class.
		/// </summary>
		/// <param name="designer"></param>
		public PatOrdenTrabajoOperacionDataSourceActionList(PatOrdenTrabajoOperacionDataSourceDesigner designer) : base(designer.Component)
		{
			_designer = designer;
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public PatOrdenTrabajoOperacionSelectMethod SelectMethod
		{
			get { return _designer.SelectMethod; }
			set { _designer.SelectMethod = value; }
		}

		/// <summary>
		/// Returns the collection of <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// objects contained in the list.
		/// </summary>
		/// <returns>A <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// array that contains the items in this list.</returns>
		public override DesignerActionItemCollection GetSortedActionItems()
		{
			DesignerActionItemCollection items = new DesignerActionItemCollection();
			items.Add(new DesignerActionPropertyItem("SelectMethod", "Select Method", "Methods"));
			return items;
		}
	}

	#endregion PatOrdenTrabajoOperacionDataSourceActionList
	
	#endregion PatOrdenTrabajoOperacionDataSourceDesigner
	
	#region PatOrdenTrabajoOperacionSelectMethod
	
	/// <summary>
	/// Enumeration of method names available for the PatOrdenTrabajoOperacionDataSource.SelectMethod property.
	/// </summary>
	public enum PatOrdenTrabajoOperacionSelectMethod
	{
		/// <summary>
		/// Represents the Get method.
		/// </summary>
		Get,
		/// <summary>
		/// Represents the GetAll method.
		/// </summary>
		GetAll,
		/// <summary>
		/// Represents the GetPaged method.
		/// </summary>
		GetPaged,
		/// <summary>
		/// Represents the Find method.
		/// </summary>
		Find,
		/// <summary>
		/// Represents the GetByIdDocumentoNumeroOrdenIdOperacion method.
		/// </summary>
		GetByIdDocumentoNumeroOrdenIdOperacion,
		/// <summary>
		/// Represents the GetByIdCargoOperacion method.
		/// </summary>
		GetByIdCargoOperacion,
		/// <summary>
		/// Represents the GetByIdCausalGarantia1 method.
		/// </summary>
		GetByIdCausalGarantia1,
		/// <summary>
		/// Represents the GetByIdCausalGarantia2 method.
		/// </summary>
		GetByIdCausalGarantia2,
		/// <summary>
		/// Represents the GetByIdCausalGarantia3 method.
		/// </summary>
		GetByIdCausalGarantia3,
		/// <summary>
		/// Represents the GetByIdCausalGarantia4 method.
		/// </summary>
		GetByIdCausalGarantia4,
		/// <summary>
		/// Represents the GetByIdEmpleado method.
		/// </summary>
		GetByIdEmpleado,
		/// <summary>
		/// Represents the GetByIdEstadoOperacion method.
		/// </summary>
		GetByIdEstadoOperacion,
		/// <summary>
		/// Represents the GetByIdDocumentoNumeroOrden method.
		/// </summary>
		GetByIdDocumentoNumeroOrden,
		/// <summary>
		/// Represents the GetByIdOperacion method.
		/// </summary>
		GetByIdOperacion
	}
	
	#endregion PatOrdenTrabajoOperacionSelectMethod

	#region PatOrdenTrabajoOperacionFilter
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilter&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatOrdenTrabajoOperacion"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatOrdenTrabajoOperacionFilter : SqlFilter<PatOrdenTrabajoOperacionColumn>
	{
	}
	
	#endregion PatOrdenTrabajoOperacionFilter

	#region PatOrdenTrabajoOperacionExpressionBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlExpressionBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatOrdenTrabajoOperacion"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatOrdenTrabajoOperacionExpressionBuilder : SqlExpressionBuilder<PatOrdenTrabajoOperacionColumn>
	{
	}
	
	#endregion PatOrdenTrabajoOperacionExpressionBuilder	

	#region PatOrdenTrabajoOperacionProperty
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ChildEntityProperty&lt;PatOrdenTrabajoOperacionChildEntityTypes&gt;"/> class
	/// that is used exclusively with a <see cref="PatOrdenTrabajoOperacion"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatOrdenTrabajoOperacionProperty : ChildEntityProperty<PatOrdenTrabajoOperacionChildEntityTypes>
	{
	}
	
	#endregion PatOrdenTrabajoOperacionProperty
}

